/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.helper;

import java.util.ArrayList;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.CollaborationUse;
import org.eclipse.uml2.uml.Component;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.GraphEdgeHelper;
import com.cea.papyrus.core.helper.IGraphEdgeHelper;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;


/**
 * TODO: comment it.
 * 
 * @author PT202707
 */
public class EdgeDependencyHelper extends GraphEdgeHelper implements IGraphEdgeHelper{

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * 
	 * 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement source){
		Element eltSource=null;
		if ((source instanceof Diagram)  ){
			if (((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement();
				if (eltSource instanceof Package){return true;}
			}
		}
		if (source.getSemanticModel()!=null ){
			if (((Uml1SemanticModelBridge)source.getSemanticModel()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)source.getSemanticModel()).getElement();
				if (eltSource instanceof  org.eclipse.uml2.uml.Package){// they are  classifiers
					return true;
				}

			}
		}

		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#createGraphEdge(org.eclipse.uml2.uml.Element, com.cea.papyrus.diagraminterchange2.di2.Diagram)
	 */
	/**
	 * 
	 * 
	 * @param umlElement 
	 * @param diagram 
	 * 
	 * @return 
	 */
	@Override
	public ArrayList createGraphEdge(Element umlElement, Diagram diagram){
		Debug.debug(this,"createGraphEdge", 10);
		GraphEdge ge=(GraphEdge)this.createGraphElement(umlElement, null, null, null);
		//look for UML connected UML element
		if (!(umlElement instanceof Dependency)){
			Debug.error(this, "EdgeDependencyHelper.createGraphEdge: "+ umlElement+ "is not a dependency!", channel);
		}
		Dependency de=(Dependency)umlElement;

		Element umlsupplier=(Element)de.getSuppliers().get(0);
		Element umlsource=(Element)de.getSources().get(0);
		super.endCreateGraphElement(umlsource, umlsupplier, ge, diagram);
		return new ArrayList();
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.NodeFactory#createGraphNodeWithSemantic(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlChild 
	 * @param size 
	 * @param location 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public GraphElement createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size) {

		GraphElement graphElement=super.createGraphElement(umlChild, null, location, size);
		NodeHelper nf=new NodeHelper();
//		name of the dependency
		GraphElement graphNode=nf.createGraphElement(null, graphElement, new Point(0, 10), null);
		graphNode.setFontSize(9);
		graphNode.setIsVisible(false);

//		stereotype of dependency
		graphNode=nf.createGraphElement(null, graphElement, new Point(0, 30), null);
		graphNode.setFontSize(9);

		//the parent has to be connected in last, because a event will be triggered.
		if(parent!=null){
			parent.getContained().add(graphElement);
		}

		return graphElement;	
	}

	/////////////////////////////////////////////////////////////////////////////////
	//                       UMLManagement
	/////////////////////////////////////////////////////////////////////////////////
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#createUMLElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	@Override
	public  org.eclipse.uml2.uml.Element createUMLElement(String name, org.eclipse.uml2.uml.Element umlParent){
		Debug.debug(this,"create association", channel); //$NON-NLS-1$
		Dependency as= UMLFactory.eINSTANCE.createDependency();
		as.setName(name);
//		if (umlParent instanceof Package){
//			((Package)umlParent).getPackagedElements().add(as);
//		}
//		if (umlParent instanceof CollaborationUse){
//			((CollaborationUse)umlParent).getRoleBindings().add(as);
//		} 
//		if (umlParent instanceof Component){
//			((Component)umlParent).getPackagedElements().add(as);
//		} else {
//			// by defaut, add it to the nearest package
//			umlParent.getNearestPackage().getPackagedElements().add(as);
//		}
		
		addUMLElement(as, umlParent);
		return as;		
	}


	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#canCreateUMLElement(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		if(umlParent instanceof Package|| umlParent instanceof CollaborationUse || umlParent instanceof Component) {
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#addUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getPackagedElements().add((PackageableElement)element);
		
		} else if (umlParent instanceof Component) {
			return ((Component)umlParent).getPackagedElements().add((PackageableElement)element);
		
		} else if (umlParent instanceof CollaborationUse){
			return ((CollaborationUse)umlParent).getRoleBindings().add((Dependency)element);
		
		} else {
		// by defaut, add it to the nearest package
			return umlParent.getNearestPackage().getPackagedElements().add((PackageableElement) element);
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#removeUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getPackagedElements().remove(element);
		
		} else if(umlParent instanceof Component) {
			return ((Component)umlParent).getPackagedElements().remove((PackageableElement)element);
		
		} else if (umlParent instanceof CollaborationUse){
			return ((CollaborationUse)umlParent).getRoleBindings().remove(element);
		
		} else {
		// by defaut, remove it from the nearest package
			return umlParent.getNearestPackage().getPackagedElements().remove((PackageableElement) element);
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#getBaseString()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public String getBaseString() {
		return "Dependency"; //$NON-NLS-1$
	}

	/////////////////////////////////////////////////////////////////////////////////
	//                       LINK MANAGEMENT
	/////////////////////////////////////////////////////////////////////////////////

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#connect(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param target 
	 * @param link 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean connect(Element source, Element target, Element link) {
		Dependency dependency=(Dependency)link;
		if(target instanceof ElementImport){
			PackageableElement ImportedTarget = (PackageableElement)((ElementImport)target).getImportedElement();
			dependency.setName("From" + ((NamedElement)source).getName()+ "to"+((NamedElement)ImportedTarget).getName());
			dependency.getSuppliers().clear();
			dependency.getSuppliers().add(ImportedTarget);
			//		target
		}else {
			dependency.setName("From" + ((NamedElement)source).getName()+ "to"+((NamedElement)target).getName());
			dependency.getSuppliers().clear();
			dependency.getSuppliers().add((NamedElement)target);
			//		target
		}
		
		dependency.getClients().clear();
		dependency.getClients().add((NamedElement)source);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.GraphEdgeFactory#deconnect(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param target 
	 * @param link 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean deconnect(Element source, Element target, Element link){
		Dependency dependency=(Dependency)link;
		dependency.setName(getNewUMLElementName(link.getOwner()));
		dependency.getSuppliers().remove(target);
		//		target
		dependency.getClients().remove(source);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.IGraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode, com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */	
	/**
	 * 
	 * 
	 * @param target 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canConnect(Element source,Element target){
		// if no source or no target found
		if ((source == null) && (target == null)) {
			return false;
		}
		if ((source != null) && (target == null)) {
			if(source instanceof NamedElement){
				return true;
			}
		}
		// if target is the source
		if (source ==target) {
			return false;
		}
		// Test source and target types
		if (source instanceof NamedElement)
		{
			if (target instanceof ElementImport){
				if (((ElementImport)target).getImportedElement() instanceof NamedElement) {
					return true;				
				}
			}
			if (target instanceof NamedElement) {
				return true;
			}
		}

		return false;
	}
}
